<script setup>
import { ref, onMounted } from 'vue'
import { getCurrentInstance } from 'vue'

// 获取全局属性中的Socket实例
const { proxy } = getCurrentInstance()
const socket = proxy.$socket

// 天气数据
const weatherData = ref(null)
const errorMessage = ref('')
const loading = ref(false)

// 获取明天的天气数据
const getTomorrowWeather = async () => {
  try {
    loading.value = true
    const response = await fetch('http://localhost:3000/api/weather/tomorrow')
    if (!response.ok) throw new Error('Failed to fetch weather data')
    weatherData.value = await response.json()
  } catch (error) {
    errorMessage.value = error.message
    setTimeout(() => errorMessage.value = '', 3000)
  } finally {
    loading.value = false
  }
}

// 生命周期钩子：组件挂载时获取天气数据
onMounted(() => {
  getTomorrowWeather()

  // 定期刷新天气数据（每小时）
  setInterval(getTomorrowWeather, 3600000)
})
</script>

<template>
  <div class="weather-forecast">
    <h2>Tomorrow's Weather Forecast</h2>

    <!-- 错误提示 -->
    <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading">Loading weather data...</div>

    <!-- 天气信息 -->
    <div v-else-if="weatherData" class="weather-info">
      <div class="weather-date">{{ new Date(weatherData.date).toLocaleDateString() }}</div>
      <div class="weather-condition">{{ weatherData.condition }}</div>
      <div class="weather-temp">Temperature: {{ weatherData.temperature }}°C</div>
      <div class="weather-humidity">Humidity: {{ weatherData.humidity }}%</div>
      <div class="weather-location">Location: {{ weatherData.location }}</div>
    </div>

    <div v-else class="no-data">
      No weather data available. Please try again later.
    </div>

    <button @click="getTomorrowWeather" class="refresh-button">
      Refresh Weather
    </button>
  </div>
</template>

<style scoped>
.weather-forecast {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h2 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.loading {
  text-align: center;
  padding: 20px;
  color: #666;
}

.weather-info {
  text-align: center;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
}

.weather-date {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #444;
}

.weather-condition {
  font-size: 24px;
  margin-bottom: 15px;
  color: #2c3e50;
}

.weather-temp {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #e74c3c;
}

.weather-humidity,
.weather-location {
  margin-bottom: 8px;
  color: #666;
}

.no-data {
  text-align: center;
  padding: 20px;
  color: #666;
}

.refresh-button {
  display: block;
  margin: 20px auto 0;
  background-color: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
}

.refresh-button:hover {
  background-color: #2980b9;
}

error-message {
  color: #ff4444;
  background-color: #ffeeee;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 15px;
  text-align: center;
}
</style>